package com.budweiser.user.controller;

import com.budweiser.commons.utils.AppUserUtil;
import com.budweiser.commons.utils.Utils;
import com.budweiser.model.log.LogAnnotation;
import com.budweiser.model.page.Page;
import com.budweiser.model.result.ResponseBase;
import com.budweiser.model.result.ResultBase;
import com.budweiser.model.user.LoginUser;
import com.budweiser.model.user.Role;
import com.budweiser.model.user.User;
import com.budweiser.user.dto.UserDto;
import com.budweiser.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
    }

    @Autowired
    private UserService userService;

    /***
     * 获取当前登录信息</br>
     *
     * @return
     */
    @GetMapping("/users/current")
    public LoginUser getLoginUser() {
        return AppUserUtil.getLoginAppUser();
    }

    @GetMapping("/users/all")
    public Page<UserDto> findUsers(@RequestParam Map<String, Object> params, String limit, String page) {
        params.put("start", page);
        params.put("length", limit);
        return userService.getUsers(params);
    }



    @GetMapping(value = "/users-anon/internal", params = "username")
    public LoginUser findByUsername(String username) {
        return userService.findByUsername(username);
    }

    @LogAnnotation(module = "新增用户")
    @PostMapping("/users/save")
    @PreAuthorize("hasAuthority('back:users:save')")
    public ResponseBase save(User user) {
        userService.save(user);
        return new ResponseBase(200, "新增成功！", null);
    }

    @LogAnnotation(module = "修改用户")
    @PostMapping("/users/update")
    @PreAuthorize("hasAuthority('back:users:update')")
    public ResponseBase update(User user) {
        userService.update(user);
        return new ResponseBase(200, "修改成功！", null);
    }

    @LogAnnotation(module = "删除用户")
    @PostMapping("/users/del")
    @PreAuthorize("hasAuthority('back:users:del')")
    public ResponseBase del(String ids) {
        userService.del(Utils.getLongs(ids));
        return new ResponseBase(200, "删除成功！", null);
    }

    @GetMapping("/users/findUserRole")
    public ResultBase<Role> findUserRole(Long userid) {
        return new ResultBase<Role>(0, "数据加载成功！", 0, userService.findUserRole(userid));
    }

    /**
     * 管理后台给用戶分配角色
     *
     * @param id            用戶id
     * @param permissionIds 角色ids
     */
    @LogAnnotation(module = "分配角色")
    @PreAuthorize("hasAuthority('back:user:role:set')")
    @PostMapping("/users/setUserToRole")
    public ResponseBase setPermissionToRole(Long userid, String roleids) {
        userService.setUserToRole(userid, Utils.getLongs(roleids));
        return new ResponseBase(200, "分配权限成功！", null);
    }

    @RequestMapping(value = "/users-anon/findUserId", method = RequestMethod.POST)
    public User findUserId(@RequestBody Long userid) {
        return userService.findUserId(userid);
    }

    /****
     * 根据部门名称查询用户列表
     *
     * @param orgname
     * @return
     */
    @PostMapping("/users/getUserByOrgName")
    public List<UserDto> getUserByOrgName(String orgname) {
        return userService.getUserByOrgName(orgname);
    }

    @PostMapping("/importUser")
    public ResponseBase importUser(@RequestParam("file") MultipartFile file) throws Exception {
//        List<UserExcel> userExcels = ExcelUtils.importExcel(file, 0, 1, UserExcel.class);
//        userService.batchSaveOrUpdate(userExcels);
//        return new ResponseBase(200, "数据导入成功！", null);
        return null;
    }

    /****
     * 根据用户id查询
     *
     * @param userid
     * @return
     */
    @PostMapping("/users-anon/getUserById")
    public User getUserById(@RequestBody Long userid) {
        return userService.findUserId(userid);
    }

    @PostMapping("/users-anon/list")
    public Page<User> list(int size, int page) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("start", size);
        params.put("length", page);
        return userService.findUsers(params);
    }

    @PostMapping("/users-anon/add")
    public  String add(User user){

        userService.add(user);
        return "success!";
    }
    @PostMapping("/users-anon/update")
    public  String mage(User user){
        userService.update(user);
        return "success!";
    }

    @GetMapping("/users-anon/test")
    public String test() {
        return "test";
    }
}
